const http = require('http');
const fs = require('fs');
const { URL } = require('url');

const json = [
  {
      "name": "UV光解净化器.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 56940
  },
  {
      "name": "储罐.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 39740
  },
  {
      "name": "单向阀.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 15402
  },
  {
      "name": "循环泵.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 52524
  },
  {
      "name": "手动阀.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 25559
  },
  {
      "name": "换热器.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 45542
  },
  {
      "name": "排放口.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 18717
  },
  {
      "name": "排放风机.gif",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 96382
  },
  {
      "name": "排放风机.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 48687
  },
  {
      "name": "收集风机.gif",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 124458
  },
  {
      "name": "收集风机.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 20796
  },
  {
      "name": "校验罐.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 16642
  },
  {
      "name": "气动阀.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 26407
  },
  {
      "name": "水喷淋.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 30896
  },
  {
      "name": "水帘柜.gif",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 408180
  },
  {
      "name": "水帘柜.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 10925
  },
  {
      "name": "活性炭吸附箱.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 19630
  },
  {
      "name": "火箭.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 30806
  },
  {
      "name": "过滤器.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 13849
  },
  {
      "name": "配气台.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 54641
  },
  {
      "name": "集气室.svg",
      "type": "file",
      "mtime": "Mon, 05 Feb 2024 07:30:30 GMT",
      "size": 7252
  }
];

const urlPrefix = "http://assets.le5lecdn.com/png/2.5D-废气治理/";

json.forEach(item => {
  const downloadUrl = urlPrefix + encodeURIComponent(item.name);
  const urlObj = new URL(downloadUrl);

  const options = {
    hostname: urlObj.hostname,
    port: 80,
    path: urlObj.pathname + urlObj.search,
    method: 'GET',
    headers: {
      'Content-Type': 'image/svg+xml'
    }
  };

  const req = http.request(options, (res) => {
    const fileName = item.name;
    const fileStream = fs.createWriteStream(fileName);

    res.pipe(fileStream);

    fileStream.on('finish', () => {
      fileStream.close();
      console.log(`${fileName} 下载完成`);
    });
  });

  req.on('error', (error) => {
    console.error(`请求失败: ${error.message}`);
  });

  req.end();
});
